package com.littcore.saap.core.web.util;

import javax.servlet.http.HttpServletRequest;

import com.littcore.saap.core.common.SaapConstants.LoginDevice;

public class RequestUtils {
    
    public static boolean isMobile(HttpServletRequest request){
        String agent=(String)request.getHeader("User-Agent");
        agent=agent.toLowerCase().trim();
        String[] mobiles=getMobiles();
        for(String mobile:mobiles){
            if(agent.indexOf(mobile)!=-1){
                return true;
            }
        }
        return false;
        
    }
    
    
    public static String[]  getMobiles(){
        String[] mobiles=new String[]{"mobile","mobi","nokia","samsung","sonyericsson","mot","blackberry","lg","htc","j2me","ucweb","opera mini","mobi","android","iphone","MicroMessenger"};
        return mobiles;
    }
    
    public static String getBrowser(HttpServletRequest request){
        String agent=(String)request.getHeader("User-Agent");
        agent=agent.toLowerCase().trim();
      /*  if(agent.indexOf("Trident")>-1){
            return "trident";//IE内核
        }
        if(agent.indexOf("Presto")>-1){
            return "presto";//opera内核
        }
        if(agent.indexOf("AppleWebKit")>-1){
            return "webKit";//苹果内核
        }
        if(!agent.matches("/AppleWebKit.*Mobile.*")||!agent.matches("/AppleWebKit/")){
            return "mobile";//是否为移动终端
        }
        if(agent.matches("/(i[^;]+;( U;)? CPU.+Mac OS X/")){
            return "ios";//ios终端
        }
        if(agent.indexOf("Android")> -1 || agent.indexOf("Linux") > -1){
            return "android";//android终端
        }
            
        if(agent.indexOf("iPhone") > -1 || agent.indexOf("Mac") > -1){
            return "iPhone";//iphone或者qq浏览器
        }
        if(agent.indexOf("iPad") > -1){
            return "iPad";//ipad
        }
        if( agent.indexOf("Safari") == -1){
            return "webApp";//
        }*/
        if(agent.contains("MicroMessenger")){
            return "wechat";
        }
        return agent;
    }
    
    public static String getLoginDevice(HttpServletRequest request){
        String browser=getBrowser(request);
        if(browser.equals(LoginDevice.WECHAT)){
            return LoginDevice.WECHAT;
        }else{
            return LoginDevice.PC;
        }
        
    }

}
